package com.yunji.framework_template.web.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fasterxml.classmate.TypeResolver;
import com.google.common.collect.Sets;

import com.yunji.framework_template.orm.persistence.vo.DataDTO;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * ClassName: SwaggerConfig Date: 2018-07-23 18:15
 *
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 */
@Configuration
// 该注解用于控制当前Configuration是否生效
@EnableSwagger2
public class SwaggerConfig {

    @Autowired
    private TypeResolver typeResolver;

    @Bean
    public Docket clientApiDocket() {
        return getDocket("ProjectModel客户端接口", "client", "com.yunji.framework_template.web.controller.api");
    }

    @Bean
    public Docket adminApiDocket() {
        return getDocket("ProjectModel管理后台接口", "admin", "com.yunji.framework_template.web.controller.admin");
    }

    private Docket getDocket(String title, String groupName, String basePackage) {
        List<ResponseMessage> responseMessageList = globalErrorResponse();
        return new Docket(DocumentationType.SWAGGER_2).produces(Sets.newHashSet(MediaType.APPLICATION_JSON_UTF8_VALUE)).apiInfo(apiInfo(title)).groupName(groupName).additionalModels(typeResolver.resolve(DataDTO.class)).globalResponseMessage(RequestMethod.GET,
                                                                                                                                                                                                                                                 responseMessageList).globalResponseMessage(RequestMethod.POST,
                                                                                                                                                                                                                                                                                            responseMessageList).globalResponseMessage(RequestMethod.PUT,
                                                                                                                                                                                                                                                                                                                                       responseMessageList).globalResponseMessage(RequestMethod.DELETE,
                                                                                                                                                                                                                                                                                                                                                                                  responseMessageList).useDefaultResponseMessages(false).select().apis(RequestHandlerSelectors.basePackage(basePackage)).build();
    }

    private ApiInfo apiInfo(String title) {
        return new ApiInfoBuilder().title(title).description("").version("2.0").build();
    }

    private List<ResponseMessage> globalErrorResponse() {
        String modelRef = "code";
        List<ResponseMessage> responseMessageList = new ArrayList<>();
        responseMessageList.add(new ResponseMessageBuilder().code(403).message("参数校验异常").responseModel(new ModelRef(modelRef)).build());
        responseMessageList.add(new ResponseMessageBuilder().code(404).message("找不到资源").responseModel(new ModelRef(modelRef)).build());

        responseMessageList.add(new ResponseMessageBuilder().code(500).message("服务器内部错误").responseModel(new ModelRef(modelRef)).build());
        return responseMessageList;
    }
}
